package com.hckjsec.official.controller;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.hckjsec.common.general.MapStringView;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.system.domain.SysPostalcode;
import com.hckjsec.system.service.ISysPostalcodeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.official.domain.OfficialCase;
import com.hckjsec.official.service.IOfficialCaseService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公司案例Controller
 * 
 * @author cyt
 * @date 2021-12-09
 */
@RestController
@RequestMapping("/official/case")
public class OfficialCaseController extends BaseController
{
    @Autowired
    private IOfficialCaseService officialCaseService;

    @Autowired
    private ISysPostalcodeService sysPostalcodeService;

    /**
     * 查询公司案例列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OfficialCase officialCase)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            officialCase.setDeptId(SecurityUtils.getDeptId());
        }
        List<OfficialCase> list = officialCaseService.selectOfficialCaseList(officialCase);
        return getDataTable(list);
    }

    /**
     * 导出公司案例列表
     */
    @Log(title = "公司案例", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OfficialCase officialCase)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            officialCase.setDeptId(SecurityUtils.getDeptId());
        }
        List<OfficialCase> list = officialCaseService.selectOfficialCaseList(officialCase);
        ExcelUtil<OfficialCase> util = new ExcelUtil<OfficialCase>(OfficialCase.class);
        return util.exportExcel(list, "case");
    }

    /**
     * 获取公司案例详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(officialCaseService.selectOfficialCaseById(id));
    }

    /**
     * 新增公司案例
     */
    @Log(title = "公司案例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OfficialCase officialCase)
    {
        SysPostalcode  sysPostalcode = sysPostalcodeService.selectSysPostalcodeById(officialCase.getCode());
        officialCase.setCodeName(sysPostalcode.getCodeName());
        officialCase.setAliasName(sysPostalcode.getAliasName());
        officialCase.setDeptId(SecurityUtils.getDeptId());
        officialCase.setCreateBy(SecurityUtils.getUsername());
        return toAjax(officialCaseService.insertOfficialCase(officialCase));
    }

    /**
     * 修改公司案例
     */
    @Log(title = "公司案例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OfficialCase officialCase)
    {
        SysPostalcode  sysPostalcode = sysPostalcodeService.selectSysPostalcodeById(officialCase.getCode());
        officialCase.setCodeName(sysPostalcode.getCodeName());
        officialCase.setAliasName(sysPostalcode.getAliasName());
        officialCase.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(officialCaseService.updateOfficialCase(officialCase));
    }

    /**
     * 删除公司案例
     */
    @Log(title = "公司案例", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(officialCaseService.deleteOfficialCaseByIds(ids));
    }

    /**
     * 获取案件类型业务范围信息
     */
    @GetMapping("/getProvinceCodeList")
    public AjaxResult getProvinceCodeList()
    {
        SysPostalcode postalcode = new SysPostalcode();
        postalcode.setCodeType("1");
        List<SysPostalcode>  plist = sysPostalcodeService.selectSysPostalcodeList(postalcode);
        List<MapStringView> htList = new ArrayList<>();
        plist.forEach(kv->{
            htList.add(new MapStringView(kv.getCode(),kv.getCodeName()));
        });
        return AjaxResult.success(htList);
    }
}
